package edu.xjdx.os.service.Impl;

import edu.xjdx.os.domain.*;
import edu.xjdx.os.mapper.CustomerMapper;
import edu.xjdx.os.mapper.GoodsMapper;
import edu.xjdx.os.mapper.OrderDetailMapper;
import edu.xjdx.os.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderDetailServiceImpl implements OrderDetailService {

    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public List<OrderDetailShow> findByOrder(Order order) {
        OrderDetailExample orderDetailExample = new OrderDetailExample();
        orderDetailExample.createCriteria().andOrderIdEqualTo(order.getOrderId());
        List<OrderDetail> orderDetails = orderDetailMapper.selectByExample(orderDetailExample);
        List<OrderDetailShow> orderDetailShows = new ArrayList<>();
        for (OrderDetail orderDetail : orderDetails) {
            OrderDetailShow orderDetailShow = new OrderDetailShow();
            orderDetailShow.setDetailId(orderDetail.getDetailId());
            orderDetailShow.setOrderId(orderDetail.getOrderId());
            orderDetailShow.setGoodsId(orderDetail.getGoodsId());
            orderDetailShow.setGoodsAmount(orderDetail.getGoodsAmount());
            Goods goods = goodsMapper.selectByPrimaryKey(orderDetail.getGoodsId());
            orderDetailShow.setOrder(order);
            orderDetailShow.setGoods(goods);
            Double price = Double.parseDouble(goods.getGoodsPrice());
            Double total = price * orderDetail.getGoodsAmount();
            orderDetailShow.setTotal(total.toString());
            orderDetailShows.add(orderDetailShow);
        }
        return orderDetailShows;
    }
}
